from django.shortcuts import render

# Create your views here.
from django.http import HttpRequest, HttpResponse, JsonResponse
'''   视图函数
文本响应
'''
def test_index1(request:HttpRequest):
	''' 文本响应'''
	data = 'Test String'
	return HttpResponse(data)

def test_index_JS(request:HttpRequest):  #第一参数必须留给 request
    ''' JSON响应 '''
    data = {'a':100, 'b':'abc'}
    return JsonResponse(data) # 可以在这加返回的状态码如    return JsonResponse(data,status=204)


from django.http.request import HttpRequest
from django.http.response import HttpResponse,HttpResponseNotAllowed,Http404,JsonResponse
from django.views.decorators.http import require_GET,require_POST,require_http_methods
@require_GET  # 限制只能是GET访问
def test_index_require_GET(request:HttpRequest):  #第一参数必须留给 request
    ''' 请求方法使用限制装饰器 '''
    return JsonResponse({'abc':123})


from django.http import HttpRequest, HttpResponse, JsonResponse
from django.views import View

''' 视图类'''
class Test_View_class1(View):
    # def dispatch(self, request, *args, **kwargs):
    #	return super().dispatch(request, *args, **kwargs)
    # 覆盖父类方法，可以使用super()再调用父类方法
    def get(self, request):  # 支持GET
        data = {'a': 100, 'b': 'abc'}
        return JsonResponse(data)
    def post(self, request):  # 支持POST
        data = {'a': 200, 'b': 'xyz'}
        return JsonResponse(data)
# 如果没有提供方法会报405错误


''' 视图类+限制装饰器'''
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.views.decorators.http import require_GET, require_http_methods
from django.views import View
from django.utils.decorators import method_decorator
@method_decorator(require_http_methods(['POST']), name='dispatch') # 可以在类前使用，应用在那个方法上，类上没有找父类的方法
class Test_View_class2(View):
    def get(self, request):  # 支持GET
        data = {'a': 100, 'b': 'abc'}
        return JsonResponse(data)
    def post(self, request):  # 支持POST
        data = {'a': 200, 'b': 'xyz'}
        return JsonResponse(data)
# 如果没有提供方法会报405错误

''' 视图类+限制装饰器'''
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.views.decorators.http import require_GET, require_http_methods
from django.views import View
class Test_View_class3(View):
    def get(self, request):  # 支持GET
        data = {'a': 100, 'b': 'abc'}
        return JsonResponse(data)
    def post(self, request):  # 支持POST
        data = {'a': 200, 'b': 'xyz'}
        return JsonResponse(data)
# 如果没有提供方法会报405错误


''' cookie
简单给request加set-cookie '''
def session1(request:HttpRequest):
    print('~' * 30 )
    print(request.COOKIES)  # 获取请求的Cookies
    print('~' * 30 )
    # 增加一个cookie set-cookie是response
    res = HttpResponse('session1 result')
    res.cookies['tttt'] = 'vvvv'
    return res

''' session 
持久化保存session'''
def session2(request:HttpRequest):
    print('~' * 30 )
    print(request.COOKIES)
    print('~' * 30 )
    # 增加一个cookie set-cookie是response
    res = HttpResponse('session1 result')
    res.cookies['tttt'] = 'aaaaaaaaa'  # set-cookie
    request.session["abc"] = 123   # abc=123 放在sessionid相关的小字典中
    request.session["xyz"] = 2000
    # set-cookie session=xxx
    print(request.session.get('abc'))
    return res


def session3(request:HttpRequest):
    '''cookie和session测试'''
    print('~' * 30)
    print(request.session.get('abc'))
    request.session.flush() # Session清除
    print(request.session.get('abc'))   # 小字典已清除
    res = HttpResponse('test')
    res.cookies['tttt'] = 'vvvvvv'
    print(request.session.keys()) # SessionStore
    request.session["xyz"] = 2000
    request.session["abc"] = 123
    print(request.session.get('abc'))   # 再次赋值
    print('~' * 30)
    return res